/**
 * 
 */
package Behaviour;

import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;

import java.awt.Point;
import java.util.Set;

import javax.swing.JPanel;

import map.Map;
import Model.CrossRoads;

/**
 * @author darkness
 * 
 */
public class SetCrossRoads extends OneShotBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map map;
	private JPanel jPanel;

	public SetCrossRoads(Agent a, Map map, JPanel jPanel) {
		super(a);
		this.map = map;
		this.jPanel = jPanel;
	}

	@Override
	public void action() {
		Set<CrossRoads> crossRoadsList = map.getCrossRoadsGraph().vertexSet();
		for (CrossRoads crossRoads : crossRoadsList) {
			Point a = crossRoads.getA();
			if (!map.getMap()[a.x][a.y].isBorder()) {
				crossRoads.setaDownEntryBlock(true);
				crossRoads.setbLeftEntryBlock(true);
				crossRoads.setcRightEntryBlock(true);
				crossRoads.setdUpEntryBlock(true);

				// CONTROLLO QUALE USCITA POSSO CHIUDERE
				Point max = crossRoads.getaDownEntry();
				int maxValue = getWeightFromPoint(crossRoads.getaDownEntry());

				if (getWeightFromPoint(crossRoads.getbLeftEntry()) > maxValue) {
					maxValue = getWeightFromPoint(crossRoads.getbLeftEntry());
					max = crossRoads.getbLeftEntry();
				}

				if (getWeightFromPoint(crossRoads.getcRightEntry()) > maxValue) {
					maxValue = getWeightFromPoint(crossRoads.getcRightEntry());
					max = crossRoads.getcRightEntry();
				}

				if (getWeightFromPoint(crossRoads.getdUpEntry()) > maxValue) {
					maxValue = getWeightFromPoint(crossRoads.getdUpEntry());
					max = crossRoads.getdUpEntry();
				}

				if (max.equals(crossRoads.getaDownEntry())) {
					crossRoads.setaDownEntryBlock(false);
				} else if (max.equals(crossRoads.getbLeftEntry())) {
					crossRoads.setbLeftEntryBlock(false);
				} else if (max.equals(crossRoads.getcRightEntry())) {
					crossRoads.setcRightEntryBlock(false);
				} else if (max.equals(crossRoads.getdUpEntry())) {
					crossRoads.setdUpEntryBlock(false);
				}

			}
		}
		jPanel.repaint();
	}

	private int getWeightFromPoint(final Point point) {
		return map.getWeightFromEdgeOfPoint(point);

	}

}
